const originalFetch = window.fetch;

function overwriteFetch() {
    window.fetch = function newFetch(url,config) {
        const startTime = Date.now();
        const reportData = {
            type: "performance",
            subType: "fetch",
            url,
            method: config?.method,
            startTime
        }
        return originalFetch(url,config).then((res) => {
            const endTime = Date.now();
            reportData.endTime = endTime;
            reportData.duration = endTime - startTime;
            const data = res.clone();
            reportData.status = data.status;
            reportData.success = data.ok;
            // 上报数据todo
            return res;
        }).catch(err=>{
            const endTime = Date.now();
            reportData.endTime = endTime;
            reportData.duration = endTime - startTime;
            reportData.status = 0;
            reportData.success = false;
            // 上报数据todo
        })
    }
}

export default function fetch() {
    overwriteFetch();
}